package com.approval.approvalapp.myself.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.WdqjBean;
import com.approval.approvalapp.util.SpUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by louyulin on 2019/4/30.
 */

public class WdqjAdapter extends RecyclerView.Adapter<WdqjAdapter.Holder> {

    List<WdqjBean> wdqjBeanList;
    Context context;
    public void setWdqjBeanList(List<WdqjBean> wdqjBeanList) {
        this.wdqjBeanList = wdqjBeanList;
        notifyDataSetChanged();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_wdqj,parent,false);
        Holder holder = new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        holder.tv1.setText(SpUtil.getString(context,SpUtil.USER_RUN_NICKNAME,"") + "的" + wdqjBeanList.get(position).getType());

        Long timeStamp = new Long(wdqjBeanList.get(position).getAdd_time()) * 1000;  //获取当前时间戳
        Date date = new Date(timeStamp);
        int dateresult = date.getYear()+1900;
        Date date2 = new Date(System.currentTimeMillis());
        int currentResult = date2.getYear() + 1900;

        if (dateresult == currentResult){
            //同年
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
            String timese = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
            holder.tv2.setText(timese);
        }else {
            //不同年
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM-dd HH:mm");
            String timese = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
            holder.tv2.setText(timese);
        }
        holder.tv3.setText("请假类型: " + wdqjBeanList.get(position).getType());
        holder.tv4.setText("开始时间: " + wdqjBeanList.get(position).getStart());
        holder.tv5.setText("结束时间: " + wdqjBeanList.get(position).getEnd());
        if (wdqjBeanList.get(position).getIs_have().equals("1") ){
            holder.tv6.setText(wdqjBeanList.get(position).getIs_have_text());
            holder.tv6.setTextColor(Color.parseColor("#1465FD"));
        }else if (wdqjBeanList.get(position).getIs_have().equals("2")){
            holder.tv6.setText(wdqjBeanList.get(position).getIs_have_text());
            holder.tv6.setTextColor(Color.parseColor("#DE463A"));
        }else {
            holder.tv6.setText(wdqjBeanList.get(position).getIs_have_text());
            holder.tv6.setTextColor(Color.parseColor("#F8AF35"));
        }
    }

    @Override
    public int getItemCount() {
        if (wdqjBeanList == null){
            return 0;
        }else {
            return wdqjBeanList.size();
        }
    }

    class Holder extends RecyclerView.ViewHolder{
        @Bind(R.id.tv1)
        TextView tv1;
        @Bind(R.id.tv2)
        TextView tv2;
        @Bind(R.id.tv3)
        TextView tv3;
        @Bind(R.id.tv4)
        TextView tv4;
        @Bind(R.id.tv5)
        TextView tv5;
        @Bind(R.id.tv6)
        TextView tv6;
        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
